//
//  StatusCell.swift
//  WKweiBo
//
//  Created by 阿拉斯加的狗 on 15/12/11.
//  Copyright © 2015年 阿拉斯加的🐶. All rights reserved.
//

import UIKit

/// 微博 Cell 中控件的间距数值
let StatusCellMargin: CGFloat = 12
/// 微博头像的宽度
let StatusCellIconWidth: CGFloat = 35

class StatusCell: UITableViewCell {

    //微博视图模型
    var viewModel: StatusViewModel? {
    
        didSet{
        
            topView.viewModel = viewModel
            
            //设置正文
            contentLabel.text = viewModel?.status.text
            
            pictureView.viewModel = viewModel
            
            pictureView.snp_updateConstraints { (make) -> Void in
                //设置自动布局
                make.size.equalTo(pictureView.bounds.size)
                
                //根据图片添加视图效果
//                let offset = viewModel?.thumbnailUrls?.count > 0 ? StatusCellMargin : 0
//                
//                make.top.equalTo(contentLabel.snp_bottom).offset(offset)
                
                
            }
            
        }
    
    }
    
    //返回视图模型对应的行高
    func rowHeight(vm: StatusViewModel) -> CGFloat {
    
        viewModel = vm
        
        contentView.layoutIfNeeded()

        //返回底部视图的最大高度
        return CGRectGetMaxY(bottonView.frame)
    }
    
    //构造函数
    override init(style: UITableViewCellStyle, reuseIdentifier: String?) {
        
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        
        //更新UI
        setUpUI()
        
        //设置选中cell的样式
        selectionStyle = .None
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //懒加载控件
    
    //顶部视图
    private lazy var topView: StatusCellTopView = StatusCellTopView()
    
    //微博正文标签
    lazy var contentLabel: UILabel = UILabel(title: "微博正文", fontSize: 15, screenInset: StatusCellMargin)
    
    //配图
    lazy var pictureView: StatusPictureView = StatusPictureView()
    
    //底部视图
    lazy var bottonView: StatusCellBottonView = StatusCellBottonView()
    
    
}

/// 设置界面
extension StatusCell {

    func setUpUI(){
    
        //1.添加控件
        contentView.addSubview(topView)
        contentView.addSubview(contentLabel)
        contentView.addSubview(pictureView)
        contentView.addSubview(bottonView)
        
        //2.自动布局
        topView.snp_makeConstraints { (make) -> Void in
            //设置上左右高的自动布局
            make.top.equalTo(contentView.snp_top)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(2 * StatusCellMargin + StatusCellIconWidth)
        }
        
        //自动布局文本内容
        contentLabel.snp_makeConstraints { (make) -> Void in
            //设置上左右的自动布局
            make.top.equalTo(topView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left).offset(StatusCellMargin)
            
        }
        
        //配图自动布局
//        pictureView.snp_makeConstraints { (make) -> Void in
//            make.top.equalTo(contentLabel.snp_bottom).offset(StatusCellMargin)
//            make.left.equalTo(contentLabel.snp_left)
//            make.width.equalTo(200)
//            make.height.equalTo(90)
//        }
        
        //底部视图
        bottonView.snp_makeConstraints { (make) -> Void in
            make.top.equalTo(pictureView.snp_bottom).offset(StatusCellMargin)
            make.left.equalTo(contentView.snp_left)
            make.right.equalTo(contentView.snp_right)
            make.height.equalTo(44)
            
//            //指定一个向下的布局
//            make.bottom.equalTo(contentView.snp_bottom)
        }
        
    }

}
